import {mapState} from 'vuex';
import sysAppManager from "@/api/system/core/sysAppManager";
import SysAppApi from "@/api/system/core/sysApp"

export default {
    computed: {
        ...mapState({
            // 所属应用
            allApps: state => state.appSetting ? state.appSetting.allApps : {},
            user: state => state.appSetting.user,
        }),
    },
    data() {
        return {
            treeRootList:[],
            initAppManagerEnd:false
        }
    },
    created() {
        if(this.isBpmClient){
            this.initAppManagerByBpm();
        }else if(this.isMyBpmClient){
            this.initMyTaskApp();
        }else {
            this.initAppManager();
        }
    },
    methods:{
        async initMyTaskApp(){
            let self_=this;
            let params={
                type:this.readType,
            };
            let res =await  SysAppApi.getMyOwnBpmApp(params);
            if(res.success){
                self_.treeRootList=res.data;
            }
            self_.initQueryParam();
            self_.isShowManageTree=true;
        },
        async initAppManagerByBpm(){
            let self_=this;
            let params={
                appTypes:"1",
                catKey:"BPM",
                readKey:self_.readKey
            };
            //弹框选择，默认可以获取到所有应用的
            if(self_.isRootAdmin){
                params.isRootAdmin=true;
            }
            let res =await  SysAppApi.getMyOwnApp(params);
            if(res.success){
                self_.treeRootList=res.data;
            }
            self_.initQueryParam();
            self_.isShowManageTree=true;
        },
        async initAppManager(){
            let self_=this;
            self_.treeRootList.push({
                appId:"defaultTree",
                name:"平台分类",
                isAdmin:false
            });
            let isAdmin=false;
            if((this.user && this.user.admin) || this.isMyBpm ){
                this.treeRootList[0].isAdmin=true;
                isAdmin=true;
                this.isManageToTree=true;
            }
            let res =await  SysAppApi.getByCategoryOrKey({CID:'-1'});
            for(let i=0;i<res.data.length;i++){
                let app=res.data[i];
                let obj={
                    appId:app.appId,
                    name:self_.$i18(app.clientName, '应用名称'),
                    title:self_.$i18(app.clientName, '应用名称'),
                    isAdmin:isAdmin
                }
                self_.treeRootList.push(obj);
            }
            //超级管理员、平台管理员，拥有所有权限
            if(isAdmin){
                self_.initQueryParam();
                self_.isShowManageTree=true;
                return;
            }
            //是否为某应用的管理员
            // 加载当前用户的应用管理员权限
            let managers=await sysAppManager.getManagerOfCurUser();
            if(managers.data.length==0){
                self_.initQueryParam();
                self_.isShowManageTree=true;
                return;
            }
            //循环匹配对应应用管理员权限数组
            for(let auth of managers.data){
                let app =self_.treeRootList.find(item => item.appId === auth.appId);
                if(app.appId){
                    app.isAdmin=true;
                }
            }

            self_.initQueryParam();
            self_.isShowManageTree=true;
        },
        initQueryParam(){
            if(this.queryParam){
                this.queryParam['treeCatType']="allRootApp";
                this.queryParam['treeRootList']=JSON.stringify(this.getAppList());
            }
            this.initAppManagerEnd=true;
        },
        getAppList(){
            let appList={};
            for(let i=0;i<this.treeRootList.length;i++){
                let app=this.treeRootList[i];
                let obj={
                    appId:app.appId,
                    isAdmin:app.isAdmin
                }
                appList[app.appId]=obj;
            }
            return appList;
        },
        getKeyVal(prefix,key){
            if(prefix){
                return "Q_"+prefix+key;
            }
            return "Q_"+key;
        },
        loadDataBySelectTreeNode(curRow) {
            this.curTree = curRow;
            var treeId=curRow.treeId;
            let treeKey=this.getKeyVal(this.treePrefix,'TREE_ID__S_EQ');
            let appKey=this.getKeyVal(this.appPrefix,'APP_ID__S_EQ');
            var param={}
            this.queryParam[appKey]="";
            this.queryParam['treeCatType']="";
            this.queryParam['treeRootList']="";
            if (treeId!="0"){
                param[treeKey]=curRow.treeId ;
            }else {
                param[treeKey]='';
                //顶级目录（包括平台与应用目录）
                if("rootApp"!=curRow.treeCatType){
                    param['treeCatType']='allRootApp';
                    param['treeRootList']=JSON.stringify(this.getAppList());
                }
            }
            if("rootApp"==curRow.treeCatType){
                param = {
                    'treeCatType':'customApp',
                    'treeRootList':JSON.stringify(this.getAppList())
                };
                param[treeKey]='';

                if("defaultTree"==curRow.appId){
                    //顶级平台目录
                    param['treeCatType']='defaultApp';
                }else {
                    param[appKey]=curRow.appId;
                }
            }
            this.loadData(param)
        },
        cancel() {
            let param = {
                treeCatType:"",
                treeRootList:JSON.stringify(this.getAppList())
            };
            let treeKey=this.getKeyVal(this.treePrefix,'TREE_ID__S_EQ')
            let appKey=this.getKeyVal(this.appPrefix,'APP_ID__S_EQ');
            if(this.queryParam.treeCatType ||
                this.queryParam[treeKey] ||
                this.queryParam[appKey]){
                param.treeCatType=this.queryParam.treeCatType;
                param[treeKey]=this.queryParam[treeKey];
                param[appKey]=this.queryParam[appKey];
            }
            this.queryParam = {
                ...param
            };
            let appId=this.curApp.appId;
            if(this.hasAppId && appId){
                this.queryParam.Q_APP_ID__S_EQ=appId;
            }
            this.$nextTick(() => {
                this.$refs.table.loadData();
            })
        },
        loadDataAgen(){
            if(!this.isInit){
                return;
            }
            this.isInit=false;
            let param = {
                'treeCatType':'allRootApp',
                'treeRootList':JSON.stringify(this.getAppList())
            };
            let treeKey=this.getKeyVal(this.treePrefix,'TREE_ID__S_EQ');
            param[treeKey]="";
            this.loadData(param);
        },
    }
}